package com.odd.webapp.server;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.odd.webapp.businessobjects.*;
import com.odd.webapp.client.*;

import javax.jdo.Query;
import java.util.*;

/**
 * TODO Open_data_dictionaryImpl class-level Javadoc.
 *
 * <p>...</p>
 *
 */

@SuppressWarnings("serial")
public class Open_data_dictionaryServiceImpl extends RemoteServiceServlet implements Open_data_dictionaryService {

  public Field[] testServer(String input) {
    String serverInfo = getServletContext().getServerInfo();
    String userAgent = getThreadLocalRequest().getHeader("User-Agent");

    PersistenceManager pm = PMF.get().getPersistenceManager();

    Category c1 = new Category("com.odd.entity", "person");
    Field f1 = new Field("com.odd.entity.person", "FirstName", "string", 10, 1);

    try {
      pm.makePersistent(c1);
      pm.makePersistent(f1);
    } finally {
      //pm.close();
    }

    //Query query = pm.newQuery("select from Field");

    Query query = pm.newQuery(Field.class);

    List<Field> results = (List<Field>) query.execute();

    Iterator iter = results.iterator();
    Field[] fs = new Field[results.size()];
    int i = 0;
    while (iter.hasNext()) {
      Field ff = (Field) iter.next();
      System.out.println(ff.getName());
      fs[i] = ff;
      i++;
    }

    return fs;
  }


  public void createCategory(Category aCategory) {
    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
      pm.makePersistent(aCategory);
    } finally {
      pm.close();
    }
  }


  public Category[] getCategorys(String aParentName) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query query = pm.newQuery(Category.class);
    query.setFilter("parentName == '" + aParentName+"'");
    query.setOrdering("name");

    System.out.println("...searching for categorys");
    List<Category> results = (List<Category>) query.execute();
    System.out.println("...found " + results.size() + " categorys");
    Iterator iter = results.iterator();
    Category[] fs = new Category[results.size()];

    int i = 0;
    while (iter.hasNext()) {
      Category ff = (Category) iter.next();
      System.out.println(ff.getName());
      fs[i] = ff;
      i++;
    }

    return fs;
  }


  public void createField(Field aField) {
    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
      pm.makePersistent(aField);
    } finally {
      pm.close();
    }
  }


  public Field[] getFields(String aCategoryName) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query query = pm.newQuery(Field.class);
    //query.setFilter("categoryName == "+categoryName);
    query.setOrdering("name");

    System.out.println("...searching for fields");
    List<Field> results = (List<Field>) query.execute();
    System.out.println("...found " + results.size() + " fields");
    Iterator iter = results.iterator();
    Field[] fs = new Field[results.size()];

    int i = 0;
    while (iter.hasNext()) {
      Field ff = (Field) iter.next();
      System.out.println(ff.getName());
      fs[i] = ff;
      i++;
    }

    return fs;
  }

}